package com.jimmy.medical.service.impl;

import com.jimmy.medical.entity.*;
import com.jimmy.medical.enums.OrderTypeEnum;
import com.jimmy.medical.enums.PictureUseTypeEnum;
import com.jimmy.medical.model.OrderReport;
import com.jimmy.medical.service.IPhysicalCheckFlowOrderService;
import com.jimmy.medical.model.SignInfo;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Qualifier("physicalCheckFlowOrderService")
public class PhysicalCheckFlowOrderServiceImpl extends FlowOrderServiceImpl
        implements IPhysicalCheckFlowOrderService {

    @Override
    public Long saveAffirmInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "确认接单", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveSignInfo(SignInfo info) {
        return saveSignInfo(info, 2);
    }

    @Override
    public Long saveSignCheckInfo(Long flowOrderId, Long userId, String photoIds) {
        FlowOrder flowOrder = process(flowOrderId, userId, "备单核验", 3);
        String[] idArray = photoIds.split(",");
        for(String id : idArray) {
            UploadFile photo = uploadFileRepository.findOne(Long.valueOf(id));
            photo.setUseType(PictureUseTypeEnum.SIGN_CHECK.getKey());
            photo.setRelatedKey(flowOrder.getOrder().getId().toString());
            uploadFileRepository.save(photo);
        }
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveSignNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话通知备单成功", 4);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveSignMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getFlowOrderId(), message.getUserId(), "短信通知备单成功", 5);
        return getCountDown(flowOrder);
    }

    @Override
    public FlowOrder submitSign(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成备单", 6);
        return getNextFlowOrder(flowOrder);
    }

    @Override
    public Long saveAcceptInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "陪检接号", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话通知体检", 2);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getFlowOrderId(), message.getUserId(), "短信通知体检", 3);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveCollatingInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "整理客户资料", 4);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveAppearance(Long flowOrderId, Long userId, Long photoId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "自检仪容", 5);
        UploadFile photo = uploadFileRepository.findOne(photoId);
        photo.setUseType(PictureUseTypeEnum.CHECK_APPEARANCE.getKey());
        photo.setRelatedKey(flowOrder.getOrder().getId().toString());
        uploadFileRepository.save(photo);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveFeeProcess(Long flowOrderId, Long userId) {
        return saveFeeProcess(flowOrderId, userId, 6);
    }

    @Override
    public Long savePhysicalCheck(Long flowOrderId, Long userId, String photoIds) {
        FlowOrder flowOrder = process(flowOrderId, userId, "陪检", 7);
        String[] idArray = photoIds.split(",");
        for(String id : idArray) {
            UploadFile photo = uploadFileRepository.findOne(Long.valueOf(id));
            photo.setUseType(PictureUseTypeEnum.PHYSICAL_CHECK.getKey());
            photo.setRelatedKey(flowOrder.getOrder().getId().toString());
            uploadFileRepository.save(photo);
        }
        return getCountDown(flowOrder);
    }

    @Override
    public FlowOrder submitPhysicalCheck(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成陪检", 8);
        return getNextFlowOrder(flowOrder);
    }

    @Override
    public Long saveReportAffirmInfo(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "取报告接单", 1);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveTakeReportInfo(Long flowOrderId, Long userId, String photoIds) {
        FlowOrder flowOrder = process(flowOrderId, userId, "取报告", 2);
        String[] idArray = photoIds.split(",");
        for(String id : idArray) {
            UploadFile photo = uploadFileRepository.findOne(Long.valueOf(id));
            photo.setUseType(PictureUseTypeEnum.TAKE_REPORT.getKey());
            photo.setRelatedKey(flowOrder.getOrder().getId().toString());
            uploadFileRepository.save(photo);
        }
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveReportNoticeInfo(PhoneCall call) {
        FlowOrder flowOrder = process(call.getOrderId(), call.getUserId(), "电话通知", 3);
        return getCountDown(flowOrder);
    }

    @Override
    public Long saveReportMessageFlow(Message message) {
        FlowOrder flowOrder = process(message.getFlowOrderId(), message.getUserId(), "短信通知", 4);
        return getCountDown(flowOrder);
    }

    @Override
    @Deprecated
    public List<OrderExceptionData> getOrderExceptionDataList(Long flowOrderId) {
        FlowOrder flowOrder = repository.findOne(flowOrderId);
        Order order = flowOrder.getOrder();

        List<OrderExceptionData> dataList = IteratorUtils.toList(orderExceptionDataRepository.findByOrder(order).iterator());
        List<Measures> measureList = measuresRepository.findByStatus("1");

        List<OrderExceptionData> showExceptionList = new ArrayList<OrderExceptionData>();
        for(OrderExceptionData o : dataList) {
            showExceptionList.add(o);
            measureList.remove(o.getMeasure());  //避免重复添加
        }

        for(Measures m : measureList) {
            OrderExceptionData o = new OrderExceptionData();
            o.setMeasure(m);
            showExceptionList.add(o);
        }

        return dataList;
    }

    @Override
    public Long savePhysicalCheckRecord(OrderExecutionRecord record, String exceptionData, String diagnoseData) {
        return saveExecutionRecord(record, exceptionData, diagnoseData, null, 5, true);
    }
    
    @Override
    public FlowOrder submitReport(Long flowOrderId, Long userId) {
        FlowOrder flowOrder = process(flowOrderId, userId, "完成取体检报告", 6);
        return getNextFlowOrder(flowOrder);
    }
}